﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SARD
{
    public partial class Statical : Form
    {
        Rectangle calc;
        Classes.dicomimage dcm;


        /// <summary>
        /// Constructor function of the class
        /// </summary>
        /// <param name="values">
        /// It takes a list of ushort values (the data to be processed)
        /// </param>
        /// <param name="calc">
        /// It takes the base rectangle that will be used to set the initial position and range.
        /// </param>
        /// <param name="width">
        /// It takes the width of the processed image (it is necessary to convert vector to a matrix)
        /// </param>
        public Statical( ref Classes.dicomimage dcm, Rectangle calc)
        {

            InitializeComponent();
            this.dcm = dcm;
            this.calc = calc;
            this.textBox1.Text = calc.Width.ToString();
            this.textBox2.Text = calc.Height.ToString();

        }


        /// <summary>
        /// This command is responsible for updating the fields of the form.
        /// During this process the max, min, average and the stddev will be calculated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            if (Convert.ToInt32(textBox1.Text) > 0 && Convert.ToInt32(textBox2.Text) > 0 
                && Convert.ToInt32(textBox1.Text) + calc.X < dcm.getwidth() 
                && Convert.ToInt32(textBox2.Text) + calc.Y < dcm.getheight())
            {

                calc.Width = Convert.ToInt32(textBox1.Text);
                calc.Height = Convert.ToInt32(textBox2.Text);
                Point maxandmin = dcm.maxandmin(calc);
                textBox3.Text = Convert.ToString(maxandmin.Y);
                textBox4.Text = Convert.ToString(maxandmin.X);
                textBox5.Text = Convert.ToString(Math.Round(dcm.average(calc), 3));
                textBox6.Text = Convert.ToString(Math.Round(dcm.stddev(calc), 3));

            }
            else
                MessageBox.Show("Parâmetro inválido, o tamanho da área de seleção deve ser " 
                    + "maior que zero e não deve exceder a área de seleção", "Erro:");

        }

        /// <summary>
        /// Updates the value of the height to be calculated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ///comunication problem I need to connect to the imagepanel.pictureBox1.redrawrect(new rectangle);
            calc.Height = Convert.ToInt32(textBox2.Text);
        }


        /// <summary>
        /// Updates the value fo the width to be calculated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            calc.Width = Convert.ToInt32(textBox1.Text);
        }

    }
}
